package com.jeesite.modules.dataScreen.web;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeesite.modules.dataScreen.domain.alarm.AlarmFormCode;
import com.jeesite.modules.dataScreen.service.HotConcernCurrenCacheService;
import com.jeesite.modules.dataScreen.service.HotConcernService;
import com.jeesite.modules.dataScreen.web.vo.Result;
import com.jeesite.modules.dataScreen.web.vo.alarm.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@Api(tags = "首页-群众关注热点事项")
@RequestMapping("/hotConcern")
public class HotConcernApi {

    @Resource
    private HotConcernService hotConcernService;
    @Resource
    private HotConcernCurrenCacheService hotConcernCurrenCacheService;
    /**
     * 超期标识常量
     */
    private final static String OVERTIME = "overtime";
    /**
     * 差评标识常量
     */
    private final static Integer EVALUATE = 0;

    @ApiOperation("今日热点事项")
    @GetMapping("/hotConcernCurrent")
    public Result<HotConcernCurrentVo> getHotConcernCurrent() {
        return Result.OK(hotConcernCurrenCacheService.getHotConcernCurrentVo());
    }

    @GetMapping("/itemConstitute")
    @ApiOperation("事项构成")
    public Result<Map<String, Integer>> getItemConstitute() {
        return Result.OK(hotConcernService.getItemConstitute());
    }

    @GetMapping("/glItemConstitute")
    @ApiOperation("普通公路事项构成")
    public Result<Map<String, Integer>> getGlItemConstitute() {
        return Result.OK(hotConcernService.getGlItemConstitute());
    }

    @GetMapping("/itemRatio")
    @ApiOperation("比率[办结比率,好差评比率]")
    public Result<AlarmRatioVo> getRatio() {
        return Result.OK(hotConcernService.getRatio());
    }

    @GetMapping("/complaint")
    @ApiOperation("投诉情况")
    public Result<List<ComplaintConsultVo>> getComplaint() {
        return Result.OK(hotConcernService.getComplaintConsult(AlarmFormCode.TX, AlarmFormCode.O_01));
    }

    @GetMapping("/consult")
    @ApiOperation("咨询情况")
    public Result<List<ComplaintConsultVo>> getConsult() {
        return Result.OK(hotConcernService.getComplaintConsult(AlarmFormCode.ZX, AlarmFormCode.O_02));
    }

    @GetMapping("/followEvent")
    @ApiOperation("关注事项数量今日趋势")
    public Result<List<FollowEventVo>> getFollowEvent() {
        return Result.OK(hotConcernService.getFollowEvent());
    }

    @GetMapping("/everyModule")
    @ApiOperation("各模块当日累计数据")
    public Result<List<EveryModuleVo>> getEveryModule() {
        return Result.OK(hotConcernService.getEveryModule());
    }

    @GetMapping("/everyModuleList")
    @ApiOperation("各模块当日累计数据详细列表")
    public Result<IPage<EveryModuleListVo>> getEveryModuleList(@RequestParam(defaultValue = "1") int pageNum,
                                                               @RequestParam(defaultValue = "10") int pageSize,
                                                               @RequestParam(required = false) String acceptunit) {
        Page<EveryModuleListVo> page = new Page<>(pageNum, pageSize);
        return Result.OK(hotConcernService.getEveryModuleList(page, acceptunit, null, null, null));
    }

    @GetMapping("/followEventList")
    @ApiOperation("关注事项数量今日趋势详细列表")
    public Result<IPage<EveryModuleListVo>> getFollowEventList(@RequestParam(defaultValue = "1") int pageNum,
                                                               @RequestParam(defaultValue = "10") int pageSize,
                                                               @RequestParam(required = false) String orderId) {
        Page<EveryModuleListVo> page = new Page<>(pageNum, pageSize);
        return Result.OK(hotConcernService.getEveryModuleList(page, null, null, null, null, null, orderId,true));
    }

    @GetMapping("/consultList")
    @ApiOperation("咨询情况详细列表")
    public Result<IPage<EveryModuleListVo>> getConsultList(@RequestParam(defaultValue = "1") int pageNum,
                                                           @RequestParam(defaultValue = "10") int pageSize,
                                                           @RequestParam(required = false) String name) {
        Page<EveryModuleListVo> page = new Page<>(pageNum, pageSize);
        return Result.OK(hotConcernService.getEveryModuleList(page, name, AlarmFormCode.ZX.getName(), null, null));
    }

    @GetMapping("/complaintList")
    @ApiOperation("投诉情况详细列表")
    public Result<IPage<EveryModuleListVo>> getComplaintList(@RequestParam(defaultValue = "1") int pageNum,
                                                             @RequestParam(defaultValue = "10") int pageSize,
                                                             @RequestParam(required = false) String name) {
        Page<EveryModuleListVo> page = new Page<>(pageNum, pageSize);
        return Result.OK(hotConcernService.getEveryModuleList(page, name, AlarmFormCode.TX.getName(), null, null));
    }

    @GetMapping("/itemConstituteList")
    @ApiOperation("事项构成详细列表")
    public Result<IPage<EveryModuleListVo>> getItemConstituteList(@RequestParam(defaultValue = "1") int pageNum,
                                                                  @RequestParam(defaultValue = "10") int pageSize,
                                                                  @RequestParam(required = false) String event) {
        Page<EveryModuleListVo> page = new Page<>(pageNum, pageSize);
        return Result.OK(hotConcernService.getEveryModuleList(page, null, event, null, null, DateUtil.date(), null, true));
    }

    @GetMapping("/enentOvertimeList")
    @ApiOperation("关注事项办理超期列表")
    @ApiImplicitParam(name = "type", value = "1:超期，2：未超期")
    public Result<IPage<EveryModuleListVo>> getEnentOvertimeList(@RequestParam(defaultValue = "1") int pageNum,
                                                                 @RequestParam(defaultValue = "10") int pageSize,
                                                                 @RequestParam(required = false) Integer type) {
        Page<EveryModuleListVo> page = new Page<>(pageNum, pageSize);
        if (Objects.equals(type, 1)) {
            return Result.OK(page);
        }
//        return Result.OK(hotConcernService.getEveryModuleList(page, null, null, OVERTIME, null));
        //默认好评100%，所以没有差评数据
        return Result.OK(hotConcernService.getEveryModuleList(page, null, null, null, null));
//        return Result.OK(page);
    }

    @GetMapping("/enentEvaluateList")
    @ApiOperation("关注事项办理好差评列表")
    @ApiImplicitParam(name = "type", value = "1:好评，2：差评")
    public Result<IPage<EveryModuleListVo>> getEnentEvaluateList(@RequestParam(defaultValue = "1") int pageNum,
                                                                 @RequestParam(defaultValue = "10") int pageSize
            , @RequestParam(required = false) Integer type) {
        Page<EveryModuleListVo> page = new Page<>(pageNum, pageSize);
        type = Objects.isNull(type) ? EVALUATE : type;
        return Result.OK(hotConcernService.getEveryModuleList(page, null, null, null, type));
    }

    @GetMapping("/enentEvaluateListPie")
    @ApiOperation("关注事项办理好差评列表饼图")
    @ApiImplicitParam(name = "type", value = "1:好评，2：差评")
    public Result<Map<String, Object>> enentEvaluateListPie(@RequestParam(required = false) Integer type) {
        return Result.OK(hotConcernService.enentEvaluateListPie(type));
    }

    @GetMapping("/enentOvertimeListPie")
    @ApiOperation("关注事项办理超期列表饼图")
    @ApiImplicitParam(name = "type", value = "1:超期，2：未超期")
    public Result<Map<String, Object>> getItemConstitutePie(@RequestParam(required = false) Integer type) {
//        return Result.OK(hotConcernService.getItemConstitutePie());
        //默认没有超期，所以没有对应数据
        if (Objects.equals(type, 1)) {
            return Result.OK(Collections.emptyMap());
        }
        return Result.OK(hotConcernService.enentEvaluateListPie(null));
    }

    @GetMapping("/complaintPie")
    @ApiOperation("投诉详情列表饼图")
    @ApiImplicitParam(name = "name", value = "单位")
    public Result<List<JSONObject>> complaintPie(String name) {
        return Result.OK(hotConcernService.getEveryPie(1, name));
    }

    @GetMapping("/consultListPie")
    @ApiOperation("咨询详情列表饼图")
    @ApiImplicitParam(name = "name", value = "单位")
    public Result<List<JSONObject>> consultListPie(String name) {
        return Result.OK(hotConcernService.getEveryPie(2, name));
    }


//    @GetMapping("/eventConstitutePie")
//    @ApiOperation("事项构成饼图")
//    public Result<Map<String, BigDecimal>> getEventConstitutePie(@RequestParam(defaultValue = "投诉") String matter) {
//        return Result.OK(hotConcernService.getEventConstitutePie(matter));
//    }

    @ApiOperation("列表通用详情")
    @GetMapping("/detail")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "myid", value = "列表myid"),
                    @ApiImplicitParam(name = "detailType", value = "列表detailType")}
    )
    public Result<List<EveryModuleDetailVo>> detail(String myid, Integer detailType) {
        return Result.OK(hotConcernService.detail(myid, detailType));
    }
}
